﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace AUV5.Web.Pages
{
    public partial class UCServiceList : UserControlBase
    {
        /// <summary>
        /// 用户ID，默认null，为空时不显示收藏按钮
        /// </summary>
        public string UserID
        {
            get { return base.PageBase.VerifiedUserID; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 参数为要绑定的ServiceList
        /// </summary>
        /// <param name="source"></param>
        public void Bind(IList<Model.ServiceInfo> source)
        {
            repService.DataSource = source;
            repService.DataBind();
        }
        /// <summary>
        /// 获取附件列表
        /// </summary>
        /// <returns></returns>
        protected string GetAttachList(string serviceID)
        {
            string link = "<a href='/Pages/ServiceDetail.aspx?serviceID=" + serviceID + "'>{0}</a>";

            IList<Model.ServiceAttachment> list = BLL.ServiceAttachmentBLL.Instance.GetListByServiceID(4, serviceID);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 4; i++)
            {
                if (i < list.Count)
                {
                    if (!string.IsNullOrEmpty(list[i].VideoPath))
                    {
                        //如果有视频则显示带播放按钮的缩略图
                        sb.Append("<span class='picborder'>");
                        sb.Append("<span class='btn_video'>");

                        sb.AppendFormat(link, string.Format("<img id='{0}' src='/!images/btn_videooff.png' onmouseover=\"MM_swapImage('{0}','','/!images/btn_videoon.png',2)\" onmouseout='MM_swapImgRestore()' />", "videoplay_" + serviceID));

                        sb.Append("</span>");
                        sb.AppendFormat(link, "<img src='" + list[i].ImagePath + "' />");
                        sb.Append("</span> ");
                    }
                    else
                    {
                        //如果没有视频则不显示视频缩略图，只显示3张图片中的N张
                        sb.AppendFormat("<span class='picborder'>{0}</span>", string.Format(link, "<img src='" + BLL.ServiceInfoBLL.Instance.GetImageThumb(list[i].ImagePath) + "' />"));
                    }
                }
                else
                {
                    //如果图片数量不足4张
                    sb.AppendFormat("<span class='picborder picborder1'>{0}</span>", "<img src='/!images/blank_pic.jpg' />");
                }
            }
            return sb.ToString();
        }

        protected string GetFirstImg(string serviceID)
        {
            string path = string.Empty; ;
            IList<Model.ServiceAttachment> list = BLL.ServiceAttachmentBLL.Instance.GetListByServiceID(4, serviceID);

            if (list.Count > 0)
            {
                foreach (Model.ServiceAttachment atta in list)
                {
                    if (string.IsNullOrEmpty(atta.VideoPath))
                    {
                        path = PageBase.GetHostAddress() + atta.ImagePath;
                        break;
                    }
                }
            }
            return path;
        }

        #region##下单     Copy From ServiceDetail
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ibtnOrder_Click(object sender, EventArgs e)
        {
            try
            {
                if (base.PageBase.IsVerified)
                {
                    LinkButton ibtn = (LinkButton)sender;
                    string returnurl = BLL.OrderInfoBLL.Instance.OrderService(ibtn.CommandArgument, base.PageBase.VerifiedUserID, null);
                    Response.Redirect(returnurl, false);
                }
                else
                {
                    Response.Redirect("~/Pages/Login.aspx?returnURL=" + Request.Url);
                }
            }
            catch (Exception ex)
            {
                Common.PageBase.log.Error("", ex);
                //log.Error("", ex);
            }
        }
        #endregion
    }
}